package org.test.analysis.sort;

import junit.framework.Assert;

import org.junit.Test;

/**
 * 工具类
 * @author game
 *
 */
public class Sutil {

	/**
	 * 字符串连接
	 * @param r1
	 * @param r2
	 * @param s 长度要为r1和r2的和
	 */
	public static void concat(char r1[],char r2[],char s[]){
		for(int i=0;i<r1.length+r2.length;i++){// 把r1 和r2 放到s中
			if(i<=r1.length-1){
				s[i]=r1[i];
			}else{
				s[i]=r2[i-r1.length];
			}
		}
	}
	
	@Test
	public void testconcat(){
		char[] r1 = {'z','b'};
		char[] r2 = { 'c', 'f','w'};
		char[]	s=new char[r1.length+r2.length];
		
		concat(r1, r2, s);
		Assert.assertEquals(new String(s).toString(), "zbcfw");
	}
	
	
	/**
	 * 字符串复制
	 * @param begin
	 * @param end
	 * @param source
	 *            　把source的部分copy到target
	 * @param target
	 *            长度已经OK了,要确保是end-begin+1
	 */
	public static void copycharArray(int begin, int end, char source[], char target[]) {
		if(begin>end){
//			System.out.println("过界了");
			return;
		}
		int i = begin;// 用来记录source的.
		int j = 0;// target下标
		while (i <= end) {
			target[j++] = source[i++];
		}
	}

	@Test
	public void testcopycharArray() {
		char[] a = { 'a', 'c', 'f', 'e' };
		int begin = 1, end = 3;
		char[] b = new char[end - begin + 1];
		copycharArray(begin, end, a, b);
		Assert.assertEquals((new String(b).toString()), "cfe");
	}

}
